from django_redis import get_redis_connection
from rest_framework import serializers
from rest_framework_jwt.settings import api_settings

from oauth.models import OAuthQQUser, OAuthSinaUser
from .utils import check_save_user_token
from users.models import User
from .utils import check_user_token


class QQAuthUserSerializer(serializers.Serializer):
    """openid 绑定用户的序列化器"""
    # mobile password sms_code access_token
    access_token = serializers.CharField(label='操作凭证')
    mobile = serializers.RegexField(label='手机号', regex=r'^1[3-9]\d{9}$')
    password = serializers.CharField(label='密码', max_length=20, min_length=8)
    sms_code = serializers.CharField(label='短信验证码')

    def validate(self, attrs):
        # 1. 把加密的openid取出来加密
        access_token = attrs.pop('access_token')
        openid = check_save_user_token(access_token)

        if openid is None:
            raise serializers.ValidationError('openid无效')
        # 1.1 把原本的openid重新添加到attrs字典(以备后期create方法中绑定使用)
        attrs['openid'] = openid
        # 2. 校验验证码
        redis_conn = get_redis_connection('verify_codes')
        mobile = attrs['mobile']
        real_sms_code = redis_conn.get('sms_%s' % mobile)
        # 向redis存储数据时都是以字符串进行存储的,取出来后都是bytes类型[bytes]

        if real_sms_code is None or attrs['sms_code'] != real_sms_code.decode():
            raise serializers.ValidationError('验证码错误')

        # 3. 拿手机号查询user表  如果能查到说明手机号已注册,如果已注册再绑定密码是否和用户匹配
        try:
            user = User.objects.get(mobile=mobile)
        except User.DoesNotExist:
            pass
        else:
            if user.check_password(attrs['password']) is False:
                raise serializers.ValidationError('密码错误')
            else:
                # 如果用户已存在,并且密码也正确 把当前的user对象存储到反序列化大字典中以备后期绑定使用
                attrs['user'] = user

            return attrs

    def create(self, validated_data):
        # 1.获取validated_data中的user,如果能取到说明用户已存在
        user = validated_data.get('user')
        if user is None:
            # 2.如果validated_data里面没有取出user,创建一个新用户
            user = User(
                username=validated_data.get('mobile'),
                mobile=validated_data.get('mobile')
            )
            user.set_password(validated_data.get('password'))
        # 3. 把openid和user绑定
        OAuthQQUser.objects.create(
            openid=validated_data.get('openid'),
            user=user
        )
        # 4. 把user返回
        return user


class WeiboOauthSerializer(serializers.Serializer):
    """微博验证序列化器"""
    # 指定模型类中没有的字段
    mobile = serializers.RegexField(label='手机号', regex=r'^1[3-9]\d{9}$')
    sms_code = serializers.CharField(label='短信验证码')
    access_token = serializers.CharField(label='操作凭证')
    password = serializers.CharField(label='密码', max_length=20, min_length=8)

    def validate(self, attrs):
        """
        验证access_token
        :param attrs:
        :return:
        """
        # 1. 把加密的access_token取出来加密
        access_token = attrs.pop('access_token')
        access_token = check_user_token(access_token)

        if access_token is None:
            raise serializers.ValidationError('access_token无效')
        # 1.1 把原本的access_token重新添加到attrs字典(以备后期create方法中绑定使用)
        attrs['access_token'] = access_token

        # 2. 校验验证码
        redis_conn = get_redis_connection('verify_codes')
        mobile = attrs['mobile']
        real_sms_code = redis_conn.get('sms_%s' % mobile)
        # 向redis存储数据时都是以字符串进行存储的,取出来后都是bytes类型[bytes]

        if real_sms_code is None or attrs['sms_code'] != real_sms_code.decode():
            raise serializers.ValidationError('验证码错误')

        # 3. 拿手机号查询user表  如果能查到说明手机号已注册,如果已注册再绑定密码是否和用户匹配
        try:
            user = User.objects.get(mobile=mobile)
        except User.DoesNotExist:
            pass
        else:
            if user.check_password(attrs['password']) is False:
                raise serializers.ValidationError('密码错误')
            else:
                # 如果用户已存在,并且密码也正确 把当前的user对象存储到反序列化大字典中以备后期绑定使用
                attrs['user'] = user

            return attrs

    def create(self, validated_data):
        """
        保存用户
        :param validated_data:
        :return:
        """
        # 1.获取validated_data中的user,如果能取到说明用户已存在
        user = validated_data.get('user')
        if user is None:
            # 2.如果validated_data里面没有取出user,创建一个新用户
            user = User(
                username=validated_data.get('mobile'),
                mobile=validated_data.get('mobile')
            )
            user.set_password(validated_data.get('password'))
        # 3. 把access_token和user绑定
        OAuthSinaUser.objects.create(
            access_token=validated_data.get('access_token'),
            user=user
        )
        # 4. 把user返回
        return user
